package exp03.tracker.controller;

import exp03.tracker.models.Region;
import exp03.tracker.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author Lg_en
 */
@RestController
public class HomeController {

    @Autowired
    private RegionService regionService;
    //构造函数
    public HomeController(RegionService regionService) {
        this.regionService = regionService;
    }
    //获取全球疫情数据
    @GetMapping("/all")
    public List<Region> getAll(Model model){
        return regionService.getAllRegions();
    }

    //获取某个国家或地区疫情数据
    @GetMapping("/{cty}")
    public Map<String,Object> getByCountry(Model model, @PathVariable("cty") String country){
        //国家为空时返回全部数据
        if( StringUtils.isEmpty(country) ){
            return  this.regionService.getAllData();
        }
        else{
            //根据国家名称获取数据
            return regionService.getByCountryName(country);
        }
    }
    //获取最新疫情数据
    @GetMapping("/latest")
    public Map<String,Object> getAllData(){
        return this.regionService.getAllData();
    }
}
